package com.zzlzd.drp.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
 * @author ceshi
 * @Title:
 * @Package
 * @Description:
 * @date 2021-3-1713:42
 */
public interface AddressRep extends JpaRepository<AddressDto,Integer>
{
    /***
     * 根据用户id 查联系人列表
     * @param id
     * @return
     */
    @Query(nativeQuery=true, value=" SELECT id,name,phone,concat(province,city,area,address) site,is_default isDefault FROM address WHERE user_id = ?1 ORDER BY is_default desc ")
    public List<Map<String,String>> findListByUserId(Integer id);

    /***
     * 查询默认地址信息
     * @param id
     * @return
     */
    @Query(nativeQuery=true, value=" SELECT * FROM address WHERE user_id = ?1 AND is_default = 1 ")
    public AddressDto findByUserIdDefault(Integer id);

    @Query(nativeQuery=true, value=" SELECT a.*,case a.province when '青海省' then 30 when '宁夏回族自治区' then 30 when '新疆维吾尔自治区' then 30 when '内蒙古自治区' then 30 when '海南省' then 30 when '西藏自治区' then 30 else 10 end freight FROM address a WHERE user_id = ?1 AND id = ?2 ")
    public Map<String,Object> getAddress(Integer user,Integer id);


    @Query(nativeQuery=true, value=" SELECT a.*,case a.province when '青海省' then 30 when '宁夏回族自治区' then 30 when '新疆维吾尔自治区' then 30 when '内蒙古自治区' then 30 when '海南省' then 30 when '西藏自治区' then 30 else 10 end freight FROM address a WHERE user_id = ?1 AND id = ?2 ")
    public Map<String,Object> findAddressById(Integer user,Integer id);

    @Query(nativeQuery=true, value=" SELECT a.*,case a.province when '青海省' then 30 when '宁夏回族自治区' then 30 when '新疆维吾尔自治区' then 30 when '内蒙古自治区' then 30 when '海南省' then 30 when '西藏自治区' then 30 else 10 end freight FROM address a WHERE user_id = ?1 AND is_default = 1 ")
    Map<String,Object> getDefaultAddress(Integer id);
}
